﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TWDECK : UIWDECK
    {
        public TWDECK()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWDECK();
            this.DataContext = MV;
            MESSAGES += "WDLOAD;WDRELOAD;";
            InitializeComponent();
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            base.LOADBYAUF();
            Preplan(dwMaster);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (!IsAB)
                return;
            if (dwMaster == null)
                return; 
            TMV.VKWDPLAND(dwMaster); 
        }
        /// <summary>
        ///每次计划，自动产生生产单号码
        /// </summary>
        public override void OnABChg(bool _IsAB)
        {
            if (!_IsAB)
                return;

            base.OnABChg(_IsAB);
        }
        public void Preplan(DataRow dwWD)
        {
            if (!IsAB)
                return;
            PM["RUNID"] = RUNID;
            //根据不同点的原料类型，需求创建不同的订单
            //在创建时，决定使用现有库存还是新订单 
            if (dwWD == null)
                return;
            PM["ORDERNUM"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDSA"].ToString() + dwWD["WDUPOS"].ToString(); ;
            //销售单规则已经创建好，这里捕捉实际值，并保存规则
            if (dwRule == null)
                return;
            MessageInfo msg = new MessageInfo();

            #region===产生首个计划规则，传递参数======
            msg = new MessageInfo();
            msg.AUFTYP = "K";
            msg.ART = dwWD["WDART"].ToString();
            msg.VAR = dwWD["WDVAR"].ToString();
            msg.FARBE = dwWD["WDFARBE"].ToString();
            msg.BESCHICH = dwWD["WDBESCHICH"].ToString();
            msg.DESS = dwWD["WDDESS"].ToString();
            msg.COLORIT = dwWD["WDCOLORIT"].ToString();
            msg.KRS = dwWD["WDAUFKRS"].ToString();
            msg.LFD = dwWD["WDAUFLFD"].ToString();
            msg.POS = dwWD["WDAUFPOS"].ToString();
            msg.P1 = dwWD["WDUPOS"].ToString();
            msg.P2 = dwWD["WDSA"].ToString();
            msg.Where = app.User;
            double QTYMAT = 0, QTYEND = 0, F02 = 0;
            QTYEND = Convert.ToDouble(dwRule["VKPQTY"].ToString());
            F02 = Convert.ToDouble(dwRule["F02"].ToString());
            QTYMAT = QTYEND - F02;//需求的成品数量
            if (QTYMAT == 0)
            {
                throw new Exception("订单数量为0,无需创建");
            }
            msg.Sender = this.RUNID;
            PM["CART"] = dwWD["WDART"].ToString() + dwWD["WDVAR"].ToString() + dwWD["WDFARBE"].ToString() + dwWD["WDBESCHICH"].ToString();
            PM["VKPAUF"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDUPOS"].ToString()+"K";
            PM["VKAUF"] = dwWD["WDAUFKRS"].ToString() + dwWD["WDAUFLFD"].ToString() + dwWD["WDAUFPOS"].ToString() + dwWD["WDUPOS"].ToString() + "K";
            msg.MSGTYPE = "RZABPNEW";
            msg.ACTION = "RZABPNEW";
            PM["ARTSTAGE"] = "F";
            PM["DELDATE"] = dwRule["DELDATE"].ToString();
            PM["VKPQTY"] = dwRule["VKPQTY"].ToString();
            PM["ORDERNUM"] = dwWD["KRS"].ToString() + dwWD["LFD"].ToString() + dwWD["POS"].ToString() + "VA ";
            PM["COLOR"] = "VA ";
            PM["AUFTYP"] = "V";
            PM["QTYEND"] = QTYMAT;
            
            TMV.VPR.NEWPLAN(PM);
            #endregion
            PM["NEWABK"] = "F";
            msg.P3 = (string)PM["NEWABK"];
            msg.ORDER = (string)PM["ORDERNUM"];
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

       
    }
}
